package com.smartEdu.service.impl.app;

import java.lang.reflect.Method;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.smartEdu.commons.CMap;
import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.RedisTool;
import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.dao.CommentsDao;
import com.smartEdu.exception.JsonException;
import com.smartEdu.exception.ServiceException;
import com.smartEdu.model.CommentsModel;
import com.smartEdu.service.CommentsService;

@Service(value="commentsServiceImpl")
public class CommentsServiceImpl extends BaseServiceImpl implements CommentsService{
	
	@Autowired
	@Qualifier(value="commentsDaoImpl")
	private CommentsDao commentsDao;
	
	@Override
	public void getCommArticleByPK(Session sess) {
		getCommentsByPK(sess,"getCommArticleByPK");
	}

	@Override
	public void getCommHomeWorkByPK(Session sess) {
		getCommentsByPK(sess,"getCommHomeWorkByPK");
	}

	@SuppressWarnings("unchecked")
	@Override
	public void getCommentsByPK(Session sess,String methodString) {
		List<CommentsModel> content=null;
		CMap map=null;
		try {
			map=SetToMap(sess);
			Method method = CommentsDao.class.getMethod(methodString, String.class);
			content=(List<CommentsModel>) method.invoke(commentsDao,map.gets("pk"));
			for (CommentsModel commentsModel : content) {
				commentsModel.setCfromname(RedisTool.findUserNameById(commentsModel.getCfromid()));
				if (commentsModel.getCtoid()!=null) {
					commentsModel.setCtoname(RedisTool.findUserNameById(commentsModel.getCtoid()));
				}
			}
			sess.put("comment", content);
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.OK);
		}
		catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("Comments业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("Comments业务流程出错");
		}
		finally {
		}
	}

}
